package hbnu.stockManage.service;

import hbnu.stockManage.model.Product;
import hbnu.stockManage.repository.ProductRepository;

import java.util.List;
import java.util.Optional;

public class ProductService {
    private final ProductRepository repository;

    public ProductService(ProductRepository repository) {
        this.repository = repository;
    }

    public List<Product> listAll() {
        return repository.findAll();
    }

    public Optional<Product> getById(int id) {
        return repository.findById(id);
    }

    public List<Product> searchByName(String keyword) {
        return repository.findByNameLike(keyword);
    }

    public Product add(String name, double price, int stock) {
        Product p = new Product(0, name, price, stock);
        return repository.add(p);
    }

    public boolean update(int id, String name, Double price, Integer stock) {
        Optional<Product> optional = repository.findById(id);
        if (optional.isEmpty()) return false;
        return repository.update(id, name, price, stock);
    }

    public boolean remove(int id) {
        Optional<Product> before = repository.findById(id);
        if (before.isEmpty()) return false;
        return repository.remove(id);
    }

    public void save() { /* JDBC模式无需显式保存 */ }
}
